package com.lm.langchain4j.config;

import com.lm.langchain4j.adapter.InMemoryVectorStore;
import com.lm.langchain4j.service.ChatAssistantDeepSeek;
import com.lm.langchain4j.service.ChatAssistantQwen;
import com.lm.langchain4j.service.DocumentService;
import com.lm.langchain4j.service.QAService;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenAiConfig {

    @Value("${openai.api-key}")
    private String apiKey;

    /**
     * 创建 Chat 模型，用于生成对话/问答
     */
    @Bean(name = "openAI")
    public OpenAiChatModel openAiChatModel() {
        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("gpt-3.5-turbo") // 或 gpt-4
                .temperature(0.7)
                .maxTokens(2048)
                .build();
    }

    @Bean(name = "qwen")
    public ChatModel chatModelQwen() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("aliQwen_api"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    // 你使用第2种类，高阶API    AiService
    @Bean(name = "qwenAssistant")
    public ChatAssistantQwen chatAssistantQwen(@Qualifier("qwen") ChatModel chatModelQwen) {
        return AiServices.create(ChatAssistantQwen.class, chatModelQwen);
    }


    /**
     * @Description: 知识出处，https://api-docs.deepseek.com/zh-cn/
     */
    @Bean(name = "deepseek")
    public ChatModel chatModelDeepSeek() {
        return
                OpenAiChatModel.builder()
                        .apiKey(System.getenv("deepseek_api"))
                        .modelName("deepseek-chat")
                        //.modelName("deepseek-reasoner")
                        .baseUrl("https://api.deepseek.com/v1")
                        .build();
    }


    @Bean(name = "deepseekAssistant")
    public ChatAssistantDeepSeek chatAssistantDeepSeek(@Qualifier("deepseek") ChatModel chatModelDeepSeek) {
        return AiServices.create(ChatAssistantDeepSeek.class, chatModelDeepSeek);
    }

    /**
     * 创建 Embedding 模型，用于文档向量化
     */
    @Bean
    public EmbeddingModel openAiEmbeddingModel(@Value("${openai.api-key}") String apiKey) {
        return OpenAiEmbeddingModel.builder()
                .apiKey(apiKey)
                .modelName("text-embedding-3-small") // 或 "text-embedding-3-large"
                .dimensions(1536)                    // 根据模型选择
                .build();
    }


    @Bean
    public InMemoryVectorStore inMemoryVectorStore() {
        return new InMemoryVectorStore();
    }
}